package formin.powerbar.core;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import formin.powerbar.gui.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import formin.powerbar.data.*;
import javax.swing.BoxLayout;

public class PowerBarGUI extends JFrame {
	
	private PowerBarCore coreRef;
	
	private JPanel mainPanel;

	private JPanel clockPanel;
	private JLabel clockNameLable;
	private JLabel clockTimeLable;
	
	
	private JPanel plugContainerPanel;
	private ArrayList<PlugPanel> plugPanelCollection;
	private int selectedPlugNumber;

	private ButtonGroup buttonGroup; 
	
	
	private SliderPanel sliderPanel;
	
	
	//constructor for PowerBarGUI
	public PowerBarGUI(PowerBarCore core, SettingsData settings){
		//set reference to core for function passing
		this.coreRef = core;
		
		//mainPanel - panel that holds all other panels
		//set vertical alignment layout
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		
		
		//clock panel - holds labels for the clock showing the current time
		clockNameLable = new JLabel("Time:");
		clockTimeLable = new JLabel();
		clockPanel = new JPanel();
		clockPanel.add(clockNameLable);
		clockPanel.add(clockTimeLable);
		
		
		
		//plug collection
		plugPanelCollection = new ArrayList<PlugPanel>();	
		plugContainerPanel = new JPanel();
		selectedPlugNumber = 0;
		
		buttonGroup = new ButtonGroup();

		
		sliderPanel = new SliderPanel(this);
		
		
		
		this.setContentPane(mainPanel);
		mainPanel.add(clockPanel);
		mainPanel.add(plugContainerPanel);
		mainPanel.add(sliderPanel);
		this.setSize(1400, 500);
		
		this.setVisible(true);
	}
	
	public int getStartSliderValue(){
		return sliderPanel.getStartSliderValue();
	}
	
	public int getEndSliderValue(){
		return sliderPanel.getEndSliderValue();
	}
	
	public void setOnOffStringFor(int plugNumb, String str){
		plugPanelCollection.get(plugNumb).setOnOffString(str);	
	}
	public void setPlugTypeFor(int plugNumb, String type){
		plugPanelCollection.get(plugNumb).setPlugType(type);
	}
	
	public void createPlugGui(Plug plug){
		PlugRadioButton plugRadioButton = new PlugRadioButton(plug.getPlugNumber(), coreRef);

		plugRadioButton.setActionCommand(String.valueOf(plug.getPlugNumber()));
		plugRadioButton.addActionListener(new MyActionListener());
		buttonGroup.add(plugRadioButton);
		plugContainerPanel.add(plugRadioButton);
		plugPanelCollection.add(plugRadioButton.getPlugPanel());
		plugPanelCollection.get(selectedPlugNumber).setFocus();
		
	}
	
	public void setTimeClockGUI(int hours, int mins){
		if (mins < 10 ){
			clockTimeLable.setText(String.valueOf(hours)+":0"+String.valueOf(mins));
		}else{
			clockTimeLable.setText(String.valueOf(hours)+":"+String.valueOf(mins));
		}
		
	}
	
	public void editOnOffCycle(int start, int end, boolean statusOn){
		coreRef.editOnOffCycle(selectedPlugNumber, start, end, statusOn);
	}
	
	public class MyActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
		      String command = (String)e.getActionCommand();
		      System.out.println(command);
		      selectedPlugNumber = Integer.parseInt(command);
		      for(int i = 0; i < plugPanelCollection.size(); i++){
					plugPanelCollection.get(i).resetFocus();
		      }
			plugPanelCollection.get(selectedPlugNumber).setFocus();
		}
	}
	

}

